package uz.edo.db.manager.core;


import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import uz.edo.db.utils.DBObject;
import uz.edo.db.utils.HibernateUtil;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/14/12
 * Time: 2:22 AM
 * To change this template use File | Settings | File Templates.
 */
public abstract class DBManager implements Serializable {
    private Class objectClass;

    public DBManager(Class objectClass) {
        this.objectClass = objectClass;
    }
    
    public synchronized void saveOrUpdate(Object obj) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            getSession().saveOrUpdate(obj);
            HibernateUtil.commitTransaction();
        } catch (Throwable e) {
            HibernateUtil.rollbackTransaction();
            throw new Exception(e.getMessage(), e);
        }
    }

    public synchronized void delete(Object obj) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            getSession().delete(obj);
            HibernateUtil.commitTransaction();
        } catch (Throwable e) {
            HibernateUtil.rollbackTransaction();
            throw new Exception(e.getMessage(), e);
        }
    }

    public synchronized void create(Object obj) throws Exception {
        try {
            getSession().save(obj);
        } catch (HibernateException e) {
            throw new Exception(e.getMessage(), e);
        }
    }
    
     public synchronized void remove(Object obj) throws Exception {
        try {
            getSession().delete(obj);
        } catch (HibernateException e) {
            throw new Exception("107 Exception in edsManager.remove(): " + e.getMessage());
        }
    }

    public synchronized void update(Object obj) throws Exception {
        try {
            HibernateUtil.beginTransaction();
            getSession().update(obj);
            HibernateUtil.commitTransaction();
        } catch (HibernateException e) {
            HibernateUtil.rollbackTransaction();
            throw new Exception("105 Exception in edsManager.update(): " + e.getMessage());
        }
    }

    public DBObject get(Integer objectID) throws Exception {
        return (DBObject) getSession().get(this.objectClass, objectID);
    }
    
    public DBObject get(String objectID) throws Exception {
        Integer id = Integer.valueOf(objectID);
        return get(id);
    }
    
    public List getAll() throws Exception {
        return getSession().createCriteria(objectClass).list();
    }

    public DBObject getObject(Integer objectID) throws Exception {
        return get(objectID);
    }
    
    protected Query createQuery(String sql) throws Exception {
        return getSession().createQuery(sql);
    }
    
    protected SQLQuery createSqlQuery(String sql) throws Exception {
        return getSession().createSQLQuery(sql);
    }
    
    private Session getSession(){
        return HibernateUtil.getSession();
    }
}

